package com.zodiac.app.szse.d.d0602.d060201;

import com.github.lzm320a99981e.component.dataautomation.Processors;
import com.github.lzm320a99981e.component.dataautomation.Transfer;
import com.zodiac.app.szse.configuration.CustomizeApplicationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

@Processors(D060201Processor.class)
@Component
public class D060201Transfer implements Transfer<List<D060201Model>> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CustomizeApplicationProperties properties;

    @Override
    public void transfer(List<D060201Model> request) {
        if (Objects.isNull(request) || request.isEmpty()) {
            return;
        }

        final String reportTablePrefix = properties.getSzse().getReportTablePrefix();
        for (D060201Model model : request) {
            String table = reportTablePrefix + model.getReportYear() + D060201Extractor.ReportCategory.valueOf(model.getReportCategory()).getMonth();
            // 根据股票代码判断次报告是否已经存在
            D060201Model existed = mongoTemplate.findOne(Query.query(Criteria.where("stockCode").is(model.getStockCode())), model.getClass(), table);
            if (Objects.nonNull(existed)) {
                continue;
            }
            mongoTemplate.save(model, table);
        }
    }
}
